<?php
defined("_VALID_ACCESS") || die('Direct access forbidden');
class SCTV_MaintenanceInformationRequest extends Module { 
	public function body() {		
  			$this->rb = $this->init_module('Utils/RecordBrowser','SCTV_MaintenanceInformationRequest','SCTV_MaintenanceInformationRequest');
			$MaintenanceInformationRequest = array(array('<=status'=>'3'), array(), array());	
    		$this->display_module($this->rb, $MaintenanceInformationRequest, 'show_data');
    }
	public function print_request() {
			$manager =  CRM_ContactsCommon::get_my_record();
			$i_am_admin = Base_AclCommon::i_am_admin();
			
			$recs = DB::Execute("
				select  sctv_maintenanceinformationrequest_data_1.id,
						sctv_maintenance_data_1.f_maintenance_code maintenance_code,
						sctv_maintenance_data_1.f_customer_name customer_name,
						sctv_maintenance_data_1.f_address address,
						sctv_street_data_1.f_name as street,
						sctv_maintenance_data_1.f_phone phone,
						deployment_employee.id deployment_employee_id,
						deployment_employee.f_last_name || ' ' || deployment_employee.f_first_name as deployment_employee_name,
						manager_employee.f_last_name || ' ' || manager_employee.f_first_name as manager_employee_name,
						sctv_information_data_1.f_name information,
						sctv_informationgroup_data_1.f_name information_group,
						sctv_maintenanceinformationrequest_data_1.f_note note,
						sctv_maintenanceinformationrequest_data_1.f_status status
				from sctv_maintenanceinformationrequest_data_1
				join sctv_information_data_1 on sctv_information_data_1.id = sctv_maintenanceinformationrequest_data_1.f_information
				join sctv_informationgroup_data_1 on sctv_informationgroup_data_1.id = sctv_information_data_1.f_information_group  
				join sctv_maintenance_data_1 on sctv_maintenance_data_1.id = sctv_maintenanceinformationrequest_data_1.f_maintenance
				join contact_data_1 deployment_employee on deployment_employee.id = sctv_maintenance_data_1.f_deployment_employee and (%b = true or deployment_employee.f_manager = %d)
				left join contact_data_1 manager_employee on manager_employee.id = deployment_employee.f_manager 
				join sctv_street_data_1 on sctv_street_data_1.id = sctv_maintenance_data_1.f_street
				join sctv_area_data_1 on sctv_area_data_1.id = sctv_street_data_1.f_area
				where sctv_maintenanceinformationrequest_data_1.active = 1 and sctv_maintenanceinformationrequest_data_1.f_status = '0'
				order by deployment_employee.id
			", array($i_am_admin, $manager['id'])
			);
			
			foreach ($recs as $key => $value){
				$deployment_employee_id = $value['deployment_employee_id'];
				if($current_deployment_employee_id === null || $current_deployment_employee_id != $deployment_employee_id)
				{
					$count = 0;
					if($current_deployment_employee_id != null)
					{
						$this->display_module($gb);
					}
				
					$current_deployment_employee_id = $deployment_employee_id; 
					$gb = $this->init_module('Utils/GenericBrowser', null, $value['deployment_employee_name']);
					$gb->set_table_columns(array(
						array('name'=>__('Order Number'), 'width'=>'40px'),
						array('name'=>__('Status')),
						array('name'=>__('Maintenance Code')),
						array('name'=>__('Customer Name')),
						array('name'=>__('Address')),
						array('name'=>__('Phone')),
						array('name'=>__('Information')),
						array('name'=>__('Note'))					
					));
					$keymd5 = md5(time());
					
        			$_SESSION[$keymd5] = array(
         				'date'=>$date,
		        	); 
				
					$print_href = 'href="modules/SCTV/MaintenanceInformationRequest/print.php?'.http_build_query(array('key'=>$keymd5, 'cid'=>CID, 'employee'=>$deployment_employee_id)).'" target="_blank"';
					$gb->set_prefix('<a '.$print_href.'>'.$value['deployment_employee_name'] .'</a>. Quản lý: ' . $value['manager_employee_name']);
				}			
			
				$count++;
				$gb_row = $gb->get_new_row();
				$gb_row->add_data( 
					$count,
					SCTV_MaintenanceInformationRequestCommon::display_status_object($this, array('id'=>$value['id'], 'status'=>$value['status']),false,null),
					$value['maintenance_code'],
					$value['customer_name'],
					$value['address'] . ' ' . $value['street'],
					$value['phone'],
					$value['information'],
					$value['note']
				);				
			}

			if($current_deployment_employee_id != null)
			{
				$this->display_module($gb);
			}
			else{
				echo '<br><b>Không tồn tại bản yêu cầu nào.</b>';
			}
    }
	
	public function information_group()
	{
		$this->rb = $this->init_module('Utils/RecordBrowser','SCTV_InformationGroup','SCTV_InformationGroup');	
    	$this->display_module($this->rb);
	}
	public function information()
	{
		$this->rb = $this->init_module('Utils/RecordBrowser','SCTV_Information','SCTV_Information');	
    	$this->display_module($this->rb);
	}
	
  	public function caption(){
		if (isset($this->rb)) return $this->rb->caption();
	}
	public function maintenance_addon($arg){
		$rb = $this->init_module('Utils/RecordBrowser','SCTV_MaintenanceInformationRequest');
		$rb->set_defaults(array('maintenance'=>$arg['id']));
		$InformationRequest = array(array('maintenance'=>$arg['id']), array(), array());
		$this->display_module($rb,$InformationRequest,'show_data');
	}
}
?>